﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LPV_POS.Module;

namespace LPV_POS.mh_pos
{
    public partial class mh_dm_company_cauhinhmail : DevExpress.XtraEditors.XtraForm
    {
        DataTable tb;
        thuvien tv;
        string[] s;
        object[] o;
        private int f_updatedcauhinhmail(string smtpserver, string port, Boolean ssl)
        {
            int ketqua = 0;
            try
            {
               
                tv = new thuvien();
                s = new string[] { "@flag", "@SMTPSERVER", "@PORT", "@SSL_" };
                o = new object[] { 3, smtpserver, port, ssl };
                ketqua = tv.SP_Update("sp_mhcompany", s, o);
                return ketqua;
            }
            catch (Exception)
            {
                return ketqua;
            }
         }
        private DataTable f_selectcauhinhmail()
        {
            s = new string[] { "@FLAG", "@CHOOSE" };
            o = new object[] { 1, 2 };
            tv = new thuvien();
            tb = tv.SP_SelectDataTable("sp_mhcompany", s, o);
            return tb;
        }
        public mh_dm_company_cauhinhmail()
        {
            InitializeComponent();
        }

        private void bton_luu_Click(object sender, EventArgs e)
        {
            Boolean ssl = false;
            if (txt_MSTPserver.Text == "" || txt_port.Text == "")
            {
                Mod_messagebox mess = new Mod_messagebox();
                mess.messagebox_pos("Thông báo", "Nhập đầy đủ thông tin", loaithongbao.thongtin.GetHashCode());
                return;
            }
            if (check_ssl.Checked == true)
                ssl = true;
            else
                ssl = false;
            int ketqua = f_updatedcauhinhmail(txt_MSTPserver.Text, txt_port.Text, ssl);
            if (ketqua == -1)
            {
                Mod_messagebox mess = new Mod_messagebox();
                mess.messagebox_pos("Thông báo", "Cập nhật không thành công", loaithongbao.thongtin.GetHashCode());
                return;
            }
            this.Close();
        }

        private void bton_thoat_Click(object sender, EventArgs e)
        {
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn thoát?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {

                this.Close();       
            }
        }

        private void mh_dm_company_cauhinhmail_Load(object sender, EventArgs e)
        {
            
            tb = f_selectcauhinhmail();
            if (tb.Rows.Count > 0)
            {
                txt_MSTPserver.Text = tb.Rows[0]["SMTPSERVER"] + "";
                txt_port.Text = tb.Rows[0]["PORT"] + "";
                string ssl_str = tb.Rows[0]["SSL_"] + "";
                if (ssl_str == "True")
                   check_ssl.Checked = true;
                else
                    check_ssl.Checked = false;
            }
        }

        private void bton_MSTPserver_Click(object sender, EventArgs e)
        {
            txt_MSTPserver.Text = new cpublic().loadformchu_point(300, 200);
        }

        private void bton_port_Click(object sender, EventArgs e)
        {
            txt_port.Text = new cpublic().loadformso_point(500, 270);
        }
    }
}